package com.mbyte.easy.teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.entity.SysUserRoles;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.teacher.entity.Teacher;
import com.mbyte.easy.teacher.service.ITeacherService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/teacher/teacher")
public class TeacherController extends BaseController  {

    private String prefix = "teacher/teacher/";
    @Autowired
    private IUserService userService;
    @Autowired
    private ITeacherService teacherService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param teacher
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                        String createTimeSpace, String deleteTimeSpace, Teacher teacher) {
        Page<Teacher> page = new Page<Teacher>(pageNo, pageSize);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<Teacher>();
        if(!ObjectUtils.isEmpty(teacher.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",teacher.getUserId());
         }
        if(!ObjectUtils.isEmpty(teacher.getName())) {
            queryWrapper = queryWrapper.like("name",teacher.getName());
         }
        if(!ObjectUtils.isEmpty(teacher.getSex())) {
            queryWrapper = queryWrapper.like("sex",teacher.getSex());
         }
        if(!ObjectUtils.isEmpty(teacher.getAge())) {
            queryWrapper = queryWrapper.like("age",teacher.getAge());
         }
        if(!ObjectUtils.isEmpty(teacher.getPhone())) {
            queryWrapper = queryWrapper.like("phone",teacher.getPhone());
         }
        if(!ObjectUtils.isEmpty(teacher.getEmail())) {
            queryWrapper = queryWrapper.like("email",teacher.getEmail());
         }
        if(!ObjectUtils.isEmpty(teacher.getDeleteTime())) {
            queryWrapper = queryWrapper.like("delete_time",teacher.getDeleteTime());
         }
        if(!ObjectUtils.isEmpty(teacher.getDeleteStatus())) {
            queryWrapper = queryWrapper.like("delete_status",teacher.getDeleteStatus());
         }
        IPage<Teacher> pageInfo = teacherService.pageInfo(page, queryWrapper,teacher);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", teacher);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param teacher
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Teacher teacher) throws Exception {
        if(userService.selectByUsername(teacher.getUsername()) != null){
            return this.error("用户名重复！请重新输入！");
        }
        SysUser user = new SysUser();
        user.setUsername(teacher.getUsername());
        user.setPassword(teacher.getUsername());
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());
        user.setAvailable(true);
        user.setSexType(teacher.getSex());
        userService.insert(user);
        SysUser sysUser = userService.selectByUsername(teacher.getUsername());
        SysUserRoles sysUserRoles = new SysUserRoles();
        sysUserRoles.setRolesId((long) 3);
        sysUserRoles.setSysUserId(sysUser.getId());
        userService.insertuserRoles(sysUserRoles);
        teacher.setUserId(sysUser.getId());
        teacher.setDeleteStatus(0);
        return toAjax(teacherService.save(teacher));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("teacher",teacherService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param teacher
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Teacher teacher){
        Teacher byId = teacherService.getById(teacher.getId());
        SysUser sysUser = userService.selectByPrimaryKey(byId.getUserId());
        sysUser.setSexType(teacher.getSex());
        userService.updateByPrimaryKey(sysUser);
        return toAjax(teacherService.updateById(teacher));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        Teacher teacher = teacherService.getById(id);
        if(userService.selectByPrimaryKey(teacher.getUserId()) == null){
            return this.error(teacher.getName()+"用户表中查无此人！");
        }
        userService.removeUser(teacher.getUserId());
        return toAjax(teacherService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        for (int i = 0; i < ids.size(); i++) {
            Long id = ids.get(i);
            Teacher teacher = teacherService.getById(id);
            if(userService.selectByPrimaryKey(teacher.getUserId()) == null){
                return this.error(teacher.getName()+"用户表中查无此人！");
            }
            userService.removeUser(teacher.getUserId());
        }
        return toAjax(teacherService.removeByIds(ids));
    }

}

